﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.DALFactory;
using ECCIT;
using System.Linq;
using OgilvyOne.UKNow.IDAL;
using OgilvyOne.UKNow.DAL;
using OgilvyOne.UKNow.ServiceReference;
using OgilvyOne.Model;
using OgilvyOne.Utility;
using OgilvyOne.BLL;
using OgilvyOne.UKNow.Globalization;


namespace OgilvyOne.UKNow.BLL
{
    public class MapKeyWordsBLL
    {
        public int LanguageID { get; set; }
        public MapKeyWordsBLL(int langID)
        {
            this.LanguageID = langID;
        }
        //获取所有关键字
        public IList<MapKeyWordsModel> selectKeyWords()
        {
            MapKeyWordsDAL use = new MapKeyWordsDAL((LanguageType)this.LanguageID);
          ;
            return use.GetAllMapKeyWordsModels();
            
        }
        //获取所有系统的并可发布的关键字
        public IList<MapKeyWordsModel> selectKeyWordsNrelsy()
        {
            MapKeyWordsDAL use = new MapKeyWordsDAL((LanguageType)this.LanguageID);
            return use.GetAllMapKeyWordsModelsReleaseNsy();

        }
        
        //查找重复出现的关键字返回重复出现关键字与重复次数
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strtext"></param>
        /// <returns></returns>
        public Dictionary<MapKeyWordsModel,int> seleRepetKeyWordsModelList(string strtext)
        {

           IList<MapKeyWordsModel> MwmIL= selectKeyWords();
           string Vx;
           char [] Vxa;
           Dictionary<MapKeyWordsModel,int> dmm=new Dictionary<MapKeyWordsModel,int> ();
          
           foreach (MapKeyWordsModel Mwm in MwmIL)
           {
               Vx = strtext.Replace("^","").Replace(Mwm.Name, "^");
               if (Vx!=Mwm.Name)
               {
                   Vxa = Vx.ToCharArray();
                   var vxx = from vv in Vxa
                             where vv == '^'
                             select vv;
                   if (vxx.Count()!=0)
                   {
                       dmm.Add(Mwm, vxx.Count());
                   }
                  

               }
           }
           return dmm;
        }
        //根据名称获取改关键字信息
        public IList<MapKeyWordsModel> selectKeyWords(string kname)
        {
            MapKeyWordsDAL use = new MapKeyWordsDAL((LanguageType)this.LanguageID);
            return use.GetAllMapKeyWordsModelsByName(kname);

        }
        //根据关键字Kid 获取该关键字信息
        public MapKeyWordsModel selKeyWords(int Kid)
        {
            MapKeyWordsDAL use = new MapKeyWordsDAL((LanguageType)this.LanguageID);
            return use.GetMapKeyWordsModelByID(Kid);
        }
        //新增关键字
        public ResultInfo Save(MapKeyWordsModel info)
        {
            MapKeyWordsDAL use = new MapKeyWordsDAL((LanguageType)this.LanguageID);
            return use.Save(info);
        }
        //删除关键字的数据
        public ResultInfo delKeyWords(int eid)
        {
            IList<int?> list = new List<int?>();
            list.Add(eid);
            MapKeyWordsDAL use = new MapKeyWordsDAL((LanguageType)this.LanguageID);
            return use.Delete(list);
        }
        //修个单个关键字的发布状态
        public void modPubOne(int eid)
        {
            List<int?> list = new List<int?>();
            list.Add(eid);

            MapKeyWordsDAL use = new MapKeyWordsDAL((LanguageType)this.LanguageID);

            MapKeyWordsModel model = use.GetMapKeyWordsModelByID(eid); //查看该关键字的当前发布状态
            if (model.State > 0) //该关键字的当前为发布状态
            {
                use.UnPublish(list);
                //设置为该关键字的state为0 不发布 (传递参数 eid 和 int=0)
            }
            else
            {
                use.Publish(list);
                //设置该关键字的state为1 发布     (传递参数 eid 和 int=1)
            }
        }
       
    }
}
